﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataMining.Bean;
using System.Data.SqlClient;
using DataMining.DataAccess.HospitalDataSetTableAdapters;
using System.Data;

namespace DataMining.DataAccess
{
    public class ICD10Access
    {
        public long InsertList(List<ICD10> patientlist)
        {
            //通过单例模式获取系统唯一的connection
            SqlConnection connection = MyConnection.getSqlConnection();
            connection.Open();

            //设置command
            SqlCommand command = connection.CreateCommand();
            command.CommandText = "select * from ICD10";
            //定义Adapter
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adapter);
            adapter.UpdateCommand = commandBuilder.GetUpdateCommand();
            adapter.InsertCommand = commandBuilder.GetInsertCommand();
            adapter.DeleteCommand = commandBuilder.GetDeleteCommand();

            //定义数据Table
            DataTable icdTable = new DataTable("ICD10");
            adapter.Fill(icdTable);//装载数据表
            int count = 0;
            //设置TDS的数据库每次提交的数量
            int batchsiza = 10;
            adapter.UpdateBatchSize = batchsiza;
            foreach (ICD10 icd in patientlist)
            {
                count++;
                DataRow row = icdTable.NewRow();
                //通过反射机制填入row中
                RowValueFromDataTable.GetValueFromTable(icd, row);
                icdTable.Rows.Add(row);
                //
                if (count % batchsiza == 0)
                {
                    adapter.Update(icdTable);
                    icdTable.Clear();
                }
            }
            adapter.Update(icdTable);
            icdTable.Clear();
            connection.Close();
            return count;
        }
    }
}
